package br.com.rhweb.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.hibernate.Session;

import br.com.rhweb.fachada.Fachada;
import br.com.rhweb.modelos.Secretarias;
import br.com.rhweb.modelos.Vinculo;
import br.com.rhweb.util.ConexaoJDBC;
import br.com.rhweb.util.FacesContextUtil;
import br.com.rhweb.util.ReportUtil;

@ManagedBean(name="relatorioBean")
@SessionScoped
public class RelatorioBean implements Serializable{

	private String status;
	private int idSecretaria;
	private int idVinculo;
	private String nomeRelatorio;
	
	private Fachada fachada;
	private Session session;
	private List<Secretarias> secretarias;
	private List<Vinculo> vinculos;
	
	public RelatorioBean(){
		idSecretaria = 0;
		idVinculo = 0;
		status = "FALSE";
		secretarias = new ArrayList<Secretarias>();
		vinculos = new ArrayList<Vinculo>();
		status = new String();
	}
	
	public void geraRelatorio(){
		
		Map<String, Object> parametros = new HashMap<String, Object>();
		
		if(idSecretaria == 0 && idVinculo == 0){
			nomeRelatorio = "listagem_funcionarios.jasper";
			parametros.put("CONFIRMADO", isConfirmado());
		}
		else if(idSecretaria != 0 && idVinculo == 0){
			nomeRelatorio = "listagem_funcionarios_secretaria.jasper";
			parametros.put("CONFIRMADO", isConfirmado());
			parametros.put("SECRETARIA", idSecretaria);
		}
		else if(idSecretaria == 0 && idVinculo != 0){
			nomeRelatorio = "listagem_funcionarios_vinculo.jasper";
			parametros.put("CONFIRMADO", isConfirmado());
			parametros.put("VINCULO", idVinculo);			
		}		
		ReportUtil.executarRelatorio("/relatorios/"+nomeRelatorio, parametros,"PDF", ConexaoJDBC.getConnection());
	}
	
	
	
	
	public boolean isConfirmado(){
		if(status.equalsIgnoreCase("true"))
			return true;
		return false;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public int getIdSecretaria() {
		return idSecretaria;
	}

	public void setIdSecretaria(int idSecretaria) {
		this.idSecretaria = idSecretaria;
	}

	public int getIdVinculo() {
		return idVinculo;
	}

	public void setIdVinculo(int idVinculo) {
		this.idVinculo = idVinculo;
	}

	public List<Secretarias> getSecretarias() {
		
		fachada = Fachada.obterInstancia();
		session = FacesContextUtil.getRequestSession();
		
		secretarias = fachada.listar_secretariass(session);
		
		return secretarias;
	}

	public void setSecretarias(List<Secretarias> secretarias) {
		this.secretarias = secretarias;
	}

	public List<Vinculo> getVinculos() {
		
		fachada = Fachada.obterInstancia();
		session = FacesContextUtil.getRequestSession();
		
		vinculos = fachada.listar_vinculos(session);
		
		return vinculos;
	}

	public void setVinculos(List<Vinculo> vinculos) {
		this.vinculos = vinculos;
	}
	
}
